package com.lucan.tracking.plugin.visitor.method

import com.google.protobuf.Type
import org.gradle.util.TextUtil
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.commons.AdviceAdapter

/**
 * @author qihailong
 * @date 2018/7/24 下午6:28
 * @description TODO
 */

class ConfigurationMethodVisitor extends AdviceAdapter {

    private String mMethodName
    private String mBaseUrl
    private String mClassName

    ConfigurationMethodVisitor(int api, MethodVisitor mv, int access,
                               String name, String desc, String baseUrl, String className) {
        super(api, mv, access, name, desc)
        mMethodName = name
        mBaseUrl = baseUrl
        mClassName = className?.replaceAll("/", ".") ?: ""
    }

    @Override
    protected void onMethodExit(int opcode) {
        super.onMethodExit(opcode)
        if (mMethodName != "onCreate") return
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/lucan/tracking/TKMonitor",
                "getInstance", "()Lcom/lucan/tracking/TKMonitor;", false)
        mv.visitVarInsn(Opcodes.ALOAD, 0)
//        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "",
//                "getApplicationContext", "()Landroid/content/Context;", false)
//        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getDefaultInstance(),
//                "getApplicationContext", "()Landroid/content/Context;", true)
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/lucan/tracking/apt/Configuration",
                "getBaseUrl", "()Ljava/lang/String;", false)
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/lucan/tracking/TKMonitor",
                "init", "(Landroid/content/Context;Ljava/lang/String;)V", false)
    }
}
